﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Web;
using System.Web.UI.WebControls;

using CommonFramework.PageResource.Entity;
using CommonFramework.PageResource;
using CommonFramework.Storage.Cookie;
using CommonFramework.Utility;

using Xinkui.Entity.User;
using Xinkui.Common;
using Xinkui.WebModel.Helper;


namespace Xinkui.WebModel.BasicClass
{
    /// <summary>
    /// 所有页面基类
    /// </summary>
    public class PageBase : System.Web.UI.Page
    {

        public UIHelper _Helper;
        public UIHelper Helper
        {
            get
            {
                return _Helper;
            }
            set
            {
                if (_Helper == value)
                    return;
                _Helper = value;
            }
        }



        private NameValueCollection seoData;

        private Literal ltTitle, ltMeta;
        /// <summary>
        /// 是否需要登录
        /// </summary>
        protected virtual bool IsNeedLogin
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// 当前登录用户
        /// </summary>
        public LoginUserEntity LoginUser { get; set; }

        public bool HasLogin
        {
            get
            {
                return LoginUser != null && LoginUser.UserID > 0;
            }
        }

        /// <summary>
        /// 页面SEO键值对，SEO信息中使用
        /// </summary>
        public NameValueCollection SEOData
        {
            get
            {
                if (this.seoData == null)
                {
                    this.seoData = new NameValueCollection();
                }

                return this.seoData;
            }
        }


        /// <summary>
        /// 页面POST请求时数据绑定。
        /// </summary>
        protected virtual void HandlePostActions() { }

        /// <summary>
        /// 页面数据绑定。
        /// </summary>
        protected virtual void BindDataSource() { }

        protected override void OnInit(EventArgs e)
        {
            if (this.Master == null)
            {
                this.ltTitle = this.FindControl("ltTitle") as Literal;
                this.ltMeta = this.FindControl("ltMeta") as Literal;
            }
            else
            {
                this.ltTitle = this.Master.FindControl("ltTitle") as Literal;
                this.ltMeta = this.Master.FindControl("ltMeta") as Literal;
            }

            _Helper = Singleton<UIHelper>.Instance;
            base.OnInit(e);
        }

        /// <summary>
        /// 页面加载。
        /// </summary>
        protected override void OnLoad(EventArgs e)
        {
            //加载当前登录用户
            LoginUser = UserManager.GetCurLoginUser();
            if (IsNeedLogin && LoginUser == null)
            {
                //必须登录未登录，跳转至登录页
                Response.Redirect(URLHelper.LinkToLogin(this.Request.RawUrl));
                return;
            }

            if (HttpContext.Current.Request.HttpMethod.Equals("POST", StringComparison.InvariantCultureIgnoreCase))
            {
                this.HandlePostActions();
            }

            this.BindDataSource();


            this.SetSeoEntity();

            base.OnLoad(e);
        }

        /// <summary>
        /// SEO 设置
        /// </summary>
        /// <param name="seo">配置获取的SEO对象</param>
        protected virtual void SetSeoEntity()
        {
            if (this.ltTitle == null || this.ltMeta == null)
            {
                return;
            }

            if (SeoSettingManager.SeoSetting.Contains(Request.Url.AbsolutePath))
            {
                SeoSetting seo = SeoSettingManager.SeoSetting[Request.Url.AbsolutePath];


                StringBuilder pMeta = new StringBuilder();
                this.ltTitle.Text = seo.Title;

                if (!string.IsNullOrEmpty(seo.Keywords))
                {
                    pMeta.AppendLine(string.Format(@"<meta name=""keywords"" content=""{0}"" />", seo.Keywords));
                }

                if (!string.IsNullOrEmpty(seo.Description))
                {
                    pMeta.AppendLine(string.Format(@"<meta name=""Description"" content=""{0}"" />", seo.Description));
                }

                if (this.SEOData != null)
                {
                    foreach (string key in this.SEOData.Keys)
                    {
                        pMeta.Replace(string.Format("<#{0}#>", key), this.SEOData[key]);
                    }
                }

                this.ltMeta.Text = pMeta.ToString();
            }
        }
    }
}
